package edu.upenn.seas.peerreview.pr
import edu.upenn.seas.peerreview.{Id, Source, Sink}
import edu.upenn.seas.peerreview.pr.PRMessages._

class PRSimple (myId:Id, transport:Sink.T, f:Source.Factory) extends Source.T {
  var myUI : Id = null
  var okToReleaseLog : Boolean = false
 
  // work-around to the fact that pipes are one way.
  // single write made it impossible to tell which way messages were going.
 
  // the pipe mySM uses to send messages
  val sendPipe = new Sink.T {
    def apply(to: Id, msg: Message) = {
      msg match{
        case msg:OKToReleaseLog => 
            okToReleaseLog = true
        case PleaseAudit(i:Id) =>
          transport(i, PleaseSendYourLog())
        case _ => transport(to, msg)
      }
    }
  }
  val mySM = f(myId, sendPipe) 
  
  // the write method for *this* which is the pipe Transport sends messages
  // to the SM through
  def apply(from:Id, msg:Message) = {
    msg match {
      case PleaseSendYourLog() =>
        if (okToReleaseLog) 
          transport(from, HereIsMyLog(new Log()))
        else
          println ("Log requested before my UI said it was OK!")
      case HereIsMyLog(null) =>

      case HereIsMyLog(l:Log) =>
        mySM(myId, AuditResult(true))
      case PR_SEND(newMsg: Message, seq: SeqNum) =>
        mySM(from, newMsg)
      case _ => mySM(from,msg)
    }
  }
}
